<?php include HEADER; ?>
<script type="text/javascript" >
 $(document).ready(function() { 
	//choose image form edit	
	$('#photoimg1').live('change', function(){ 
		debugger;
		$("#preview1").html('');
		$("#preview1").html('<img src="<?php echo __URL_BASE ?>img/loader.gif" alt="Uploading...."  />');
		$("#imageform1").ajaxForm({
			target: '#preview1'
		}).submit();

	});
	$('#photoimg2').live('change', function(){ 
		debugger;
		$("#preview2").html('');
		$("#preview2").html('<img src="<?php echo __URL_BASE ?>img/loader.gif" alt="Uploading...."  />');
		$("#imageform2").ajaxForm({
			target: '#preview2'
		}).submit();

	});
	$('#photoimg3').live('change', function(){ 
		debugger;
		$("#preview3").html('');
		$("#preview3").html('<img src="<?php echo __URL_BASE ?>img/loader.gif" alt="Uploading...."  />');
		$("#imageform3").ajaxForm({
			target: '#preview3'
		}).submit();

	});
	$('#photoimg4').live('change', function(){ 
		debugger;
		$("#preview4").html('');
		$("#preview4").html('<img src="<?php echo __URL_BASE ?>img/loader.gif" alt="Uploading...."  />');
		$("#imageform4").ajaxForm({
			target: '#preview4'
		}).submit();

	});
	$('#photoimg5').live('change', function(){ 
		debugger;
		$("#preview5").html('');
		$("#preview5").html('<img src="<?php echo __URL_BASE ?>img/loader.gif" alt="Uploading...."  />');
		$("#imageform5").ajaxForm({
			target: '#preview5'
		}).submit();

	});
	
	$('.uupdate').click(function(e) {
		debugger;
		insert = 'true';
		tivi_id = 0;
		<?php if(isset($_REQUEST['tivi_id'])){ ?>
				insert = 'false';
				tivi_id = <?php echo $_REQUEST['tivi_id'] ?>;
		<?php } ?>
		
		trangthai = 0;
		if($('#trangthai').is(":checked")){
			trangthai = 1;
		}
		
        name = $('#tname').val();
		phangiai = $('#tphangiai').val();
		kichthuoc = $('#tkichthuoc').val();
		giathat = $('#tgiathat').val();
		giaban = $('#tgiaban').val();
		soluong = $('#tsoluong').val();
		hangsx = $('#sidhang').val();
		loaitv = $('#sidloai').val();
		images = $('#txtimg1').val();
		images2 = $('#txtimg2').val();
		images3 = $('#txtimg3').val();
		images4 = $('#txtimg4').val();
		images5 = $('#txtimg5').val(); 
		mota = CKEDITOR.instances['tmota'].getData();
		thongso = CKEDITOR.instances['tthongso'].getData();
		if(name == ''){
			alert('Tên không được để trống');
			$('#tname').focus();	
			return false;
		}
		if(phangiai == ''){
			alert('Độ phân giải không được để trống');
			$('#tphangiai').focus();	
			return false;
		}
		if(kichthuoc == ''){
			alert('Kích thươc không được để trống');	
			$('#tkichthuoc').focus();
			return false;
		}
		if(giathat == ''){
			alert('Giá thật không được để trống');	
			$('#tgiathat').focus();
			return false;
		}
		if(giaban == ''){
			alert('Giá bán không được để trống');	
			$('#tgiaban').focus();
			return false;
		}
		if(soluong == ''){
			alert('Số lượng không được để trống');	
			$('#tsoluong').focus();
			return false;
		}
		flag = false;
		$("#group .tinhnang").each(function(){
			if($(this).val() == ''){
				flag = true;
				return;
			}
			
		});
		if(flag == true){
			alert('Tính năng không được để trống');	
			return false;
		}
		var myArray = [];
		$("#group .tinhnang").each(function(){
			//alert($(this).val());
			myArray.push($(this).val());
			//alert(myArray);
		});
		$.ajax({
			url:"<?php echo __URL_BASE.'index.php?rt=admin_tivi/save' ?>",
			type:'post',
			datatype: 'html',
			data:{name:name,phangiai:phangiai,kichthuoc:kichthuoc,giathat:giathat,giaban:giaban,soluong:soluong,hangsx:hangsx,loaitv:loaitv,images:images,images2:images2,images3:images3,images4:images4,images5:images5,tinhnang:myArray,mota:mota,thongso:thongso,insert:insert,tivi_id:tivi_id,trangthai:trangthai},
			success:function(result){
				//alert(result);
				$('#a').html(result);
				if(result == '0'){
					alert('Không thể để trạng thái Hiện cho những tivi thuộc hãng sản xuất không hoạt động');
				}
				else if(result == 'true')
					location.href= '<?php echo __URL_BASE.'index.php?rt=admin_tivi' ?>';
				else alert('Có lỗi xảy ra.');
			}
		});
    });
	
	$("input[name=num]").keydown(function(event) {
		// Allow: backspace, delete, tab, escape, and enter
			if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || 
			
			event.keyCode == 27 || event.keyCode == 13 || 
			// Allow: Ctrl+A
			(event.keyCode == 65 && event.ctrlKey === true) || 
			// Allow: home, end, left, right
			(event.keyCode >= 35 && event.keyCode <= 39)) {
			// let it happen, don't do anything
				return;
			}
			else {
			// Ensure that it is a number and stop the keypress
				if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && 
			
				(event.keyCode < 96 || event.keyCode > 105 )) {
					event.preventDefault(); 
				}   
			}
		});
}); 
function removeElement(x){
		debugger;
		var name =x.name;
		//alert(name);
		$('[name='+name+']').remove();
}
$(document).on("click","#bthem",function(){
	var i = 0;
	$("#group .tinhnang").each(function(){
			i++;
	});
	$("#group").append("<br name='del"+ i +"' />");
	$("#group").append("<input id='tinhnang"+i+"' type='text' name='del"+i+"' class='text tinhnang' />");
	$("#group").append("<img src='<?php echo __URL_BASE ?>img/DeleteRed.png' name='del"+ i +"' id='del"+ i +"'  style='width:20px;cursor:pointer;position:relative;top:5px;margin-left:5px;' />");
});
$(document).on("click","#group [id^='del']",function(){
	debugger;
	var name = jQuery(this).prop("name").toLowerCase();
	$("#group [name='"+name+"']").remove();
});	
$.urlParam = function (name) {
	var results = new RegExp('[\\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href);
	return results[1] || 0;
}
</script>
<?php
 
	$TenTivi = (isset($tivi)) ? $tivi->TenTivi : '';
	$PhanGiai = (isset($tivi)) ? $tivi->PhanGiai : '';
	$KichThuoc = (isset($tivi)) ? $tivi->KichThuoc : '';
	$GiaThat = (isset($tivi)) ? $tivi->GiaThat : '';
	$GiaBan = (isset($tivi)) ? $tivi->GiaBan : '';
	$SoLuong = (isset($tivi)) ? $tivi->SoLuong : '';
	$Images = (isset($tivi)) ? $tivi->Images : '';
	$Images2 = (isset($tivi)) ? $tivi->Images2 : '';
	$Images3 = (isset($tivi)) ? $tivi->Images3 : '';
	$Images4 = (isset($tivi)) ? $tivi->Images4 : '';
	$Images5 = (isset($tivi)) ? $tivi->Images5 : '';
	$TinhNang = (isset($tivi)) ? $tivi->TinhNang : ''; //echo $TinhNang ; die;
	$MoTa = (isset($tivi)) ? $tivi->MoTa : '';
	$ThongSoKyThuat = (isset($tivi)) ? $tivi->ThongSoKyThuat : ''; //echo $ThongSoKyThuat;die;
	$idHangSanXuat = (isset($tivi)) ? $tivi->idHangSanXuat : '';
	$idLoaiTv = (isset($tivi)) ? $tivi->idLoaiTv : '';
	$TrangThai = (isset($tivi)) ? $tivi->TrangThai : '';	 
 
?>
<div >
    <table class="TableUpdate" border="1">
    
        <tr>
            <td class="Control" colspan="2">
              <ul>
                <li>
                  <label class="uupdate" >Ghi lại</label>
                </li>
                <li>
                  <label class="uback"  >Trở về</label>
                </li>
              </ul></td>
          </tr>
          <tr>
            <th>Tên :</th>
            <td><input type="text" id="tname" class="text" name="tname" value="<?php echo $TenTivi ?>" /><input type="hidden" id="tid" /></td>
          </tr>
          <tr>
            <th>Image:</th>
            <td>
           <!-- upload image1-->
            <form id="imageform1" method="post" enctype="multipart/form-data" action="<?php echo __URL_BASE.'index.php?rt=admin_tivi/ajaxImage&image=1' ?>" >
                <input type="file" name="photoimg1" id="photoimg1" style="width:90px"/>
                </form>
                <div id='preview1' >
			    <?php
					if($Images!=''){
				?>
						<img name="image1" src="<?php echo __URL_BASE.'images/'.$Images ?>" style="max-width:200px">
						<input id="txtimg1" type="hidden" name="image1" value="<?php echo $Images ?>">
						<button id="btnremove1" name="image1" style="position: absolute;margin-top: 89px;margin-left: 50px;" onclick="removeElement(this);">Xóa ảnh</button>
				<?php	
					}
				?>
                </div>
                
               <!--upload image2-->
                <form id="imageform2" method="post" enctype="multipart/form-data" action="<?php echo __URL_BASE.'index.php?rt=admin_tivi/ajaxImage&image=2' ?>" >
                <input type="file" name="photoimg2" id="photoimg2" style="width:90px"/>
                </form>
                <div id='preview2' >
				<?php
					if($Images2!=''){
				?>
						<img name="image2" src="<?php echo __URL_BASE.'images/'.$Images2 ?>" style="max-width:200px">
						<input id="txtimg2" type="hidden" name="image2" value="<?php echo $Images2 ?>">
						<button id="btnremove2" name="image2" style="position: absolute;margin-top: 89px;margin-left: 50px;" onclick="removeElement(this);">Xóa ảnh</button>
				<?php	
					}
				?>
                </div>
                
                <!--upload image3-->
                <form id="imageform3" method="post" enctype="multipart/form-data" action="<?php echo __URL_BASE.'index.php?rt=admin_tivi/ajaxImage&image=3' ?>" >
                <input type="file" name="photoimg3" id="photoimg3" style="width:90px"/>
                </form>
                <div id='preview3' >
				<?php
					if($Images3!=''){
				?>
						<img name="image3" src="<?php echo __URL_BASE.'images/'.$Images3 ?>" style="max-width:200px">
						<input id="txtimg3" type="hidden" name="image3" value="<?php echo $Images3 ?>">
						<button id="btnremove3" name="image3" style="position: absolute;margin-top: 89px;margin-left: 50px;" onclick="removeElement(this);">Xóa ảnh</button>
				<?php	
					}
				?>
                </div>
                
                <!--upload image4-->
                <form id="imageform4" method="post" enctype="multipart/form-data" action="<?php echo __URL_BASE.'index.php?rt=admin_tivi/ajaxImage&image=4' ?>" >
                <input type="file" name="photoimg4" id="photoimg4" style="width:90px"/>
                </form>
                <div id='preview4' >
				<?php
					if($Images4!=''){
				?>
						<img name="image4" src="<?php echo __URL_BASE.'images/'.$Images4 ?>" style="max-width:200px">
						<input id="txtimg4" type="hidden" name="image2" value="<?php echo $Images4 ?>">
						<button id="btnremove4" name="image4" style="position: absolute;margin-top: 89px;margin-left: 50px;" onclick="removeElement(this);">Xóa ảnh</button>
				<?php	
					}
				?>
                </div>
                
                <!--upload image5-->
                <form id="imageform5" method="post" enctype="multipart/form-data" action="<?php echo __URL_BASE.'index.php?rt=admin_tivi/ajaxImage&image=5' ?>" >
                <input type="file" name="photoimg5" id="photoimg5" style="width:90px"/>
                </form>
                <div id='preview5' >
				<?php
					if($Images5!=''){
				?>
						<img name="image5" src="<?php echo __URL_BASE.'images/'.$Images5 ?>" style="max-width:200px">
						<input id="txtimg5" type="hidden" name="image2" value="<?php echo $Images5 ?>">
						<button id="btnremove5" name="image5" style="position: absolute;margin-top: 89px;margin-left: 50px;" onclick="removeElement(this);">Xóa ảnh</button>
				<?php	
					}
				?>
                </div>
            </td>
          </tr>
          <tr>
            <th>Độ phân giải:</th>
            <td><input type="text" id="tphangiai" class="text" name="tphangiai" value="<?php echo $PhanGiai ?>" /></td>
          </tr>
          <tr>
            <th>Kích thước:</th>
            <td><input type="text" id="tkichthuoc" class="text" name="num" value="<?php echo $KichThuoc ?>" /></td>
          </tr>
          <tr>
            <th>Giá thật:</th>
            <td><input id="tgiathat" type="text"  name="num" class="text" value="<?php echo $GiaThat ?>"/></td>
          </tr>
          <tr>
            <th>Giá bán:</th>
            <td><input id="tgiaban" type="text"  name="num" class="text" value="<?php echo $GiaBan ?>"/></td>
          </tr>
          <tr>
            <th>Số lượng:</th>
            <td><input id="tsoluong" type="text"  name="num" class="text" value="<?php echo $SoLuong ?>"/></td>
          </tr>
          <tr>
            <th>Tính năng:</th>
            <td><div id="group">
			<?php
				if($TinhNang==''){
					echo '<input id="tinhnang" type="text"  class="text tinhnang"  />';
				}
				else{
					$arrTinhNang = json_decode($TinhNang);
					$i=0;
					foreach($arrTinhNang as $k=>$v){
						if($i==0){
							echo '<input id="tinhnang" type="text"  class="text tinhnang"  value="'.$v.'" />';
						}	
						else{
			?>
							<br name='del<?php echo $i ?>' />
							<input id='tinhnang<?php echo $i ?>' type='text' name='del<?php echo $i ?>' class='text tinhnang'  value='<?php echo $v; ?>'/>
							<img src='<?php echo __URL_BASE?>img/DeleteRed.png' name='del<?php echo $i ?>' id='del<?php echo $i ?>'  style='width:20px;cursor:pointer;position:relative;top:5px;margin-left:5px;' />
			<?php
						}
						$i++;
					}
				}
			?>
            	</div><button id='bthem'>Thêm</button>
            </td>
          </tr>
           <tr>
            <th>Mô tả:</th>
            <td><textarea  id="tmota" rows="20" cols="10" class="ckeditor" ><?php echo $MoTa; ?></textarea></td>
          </tr>
           <tr>
            <th>Thông số kỹ thuật:</th>
            <td><textarea  id="tthongso" rows="20" cols="10" class="ckeditor"   ><?php echo $ThongSoKyThuat; ?></textarea></td>
          </tr>
           <tr>
            <th>Hãng sản xuất:</th>
            <td>
            	<select style="width:155px;" id="sidhang">
				<?php
                	$hsxModel = new hangsanxuat();
					if(isset($_REQUEST['tivi_id']))
						$lsthang = $hsxModel->getAllHangSanXuatByWhere('','TenHang'); 
					else 
						$lsthang = $hsxModel->getAllHangSanXuatByWhere('TrangThai=1','TenHang');
					foreach($lsthang as $item){
				?>
						<option value="<?php echo $item->id ?>" <?php if($item->id == $idHangSanXuat) echo 'selected="selected"' ?> ><?php echo $item->TenHang ?></option>
				<?php	
					}
                ?>
                </select>
            </td>
          </tr>
          <tr>
            <th>Loại tivi:</th>
            <td>
            	<select style="width:155px;" id="sidloai">
				<?php
                	$loaiModel = new loaitivi();
					$lsthang = $loaiModel->getAllLoaiTiviByWhere('','TenLoai');  
					foreach($lsthang as $item){
				?>
						<option value="<?php echo $item->id ?>" <?php if($item->id == $idLoaiTv) echo 'selected="selected"' ?> ><?php echo $item->TenLoai ?></option>
				<?php
					}
                ?>
                </select>
            </td>
          </tr>
		  
		  <tr <?php if(!isset($_REQUEST['tivi_id'])){ echo "style='display:none;'";} ?>>
			<th>Trạng thái:</th>
			<td><input type="checkbox" id="trangthai" <?php if($TrangThai=='1') echo 'checked="checked"'; ?> /></td>
		  </tr>
		  
          <tr>
            <td class="Control" colspan="2">
              <ul>
                <li>
                  <label class="uupdate" >Ghi lại</label>
                </li>
                <li>
                  <label class="uback"  >Trở về</label>
                </li>
              </ul></td>
          </tr>
    </table>
    <div id="a"></div>
 </div>
<?php include FOOTER; ?>